//
//  ReciteWordPlanViewController.m
//  Learn
//
//  Created by apple on 2024/4/2.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ReciteWordPlanViewController.h"
#import "PlanView.h"
#import "ReciteViewController.h"
#import "BeiDanCiRequest.h"
#import "DayNumPickerView.h"

@interface ReciteWordPlanViewController ()
/* <#描述#> */
@property (nonatomic, strong) UIImageView    * topBgImg;
@property (nonatomic, strong) UIButton    * sureBtn;

/* bgview */
@property (nonatomic, strong) UIView    * bgView;
@property (nonatomic, strong) PlanView    * planTopView;
@property (nonatomic, strong) UIView    * lineView;
@property (nonatomic, strong) UILabel *planTitle;

@property (nonatomic, strong) UIButton    * greenBgView;
@property (nonatomic, strong) DayNumPickerView    * myPickerView;

@property (nonatomic, copy) NSString*days;
@property (nonatomic, copy) NSString*count;



@end

@implementation ReciteWordPlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.topBgImg];

    [self initCusNav];
    self.navigationBar.navTitle.text = @"制定计划";
    self.navigationBar.navTitle.textColor = UIColor.whiteColor;

    self.navigationBar.bgColor = [UIColor.whiteColor colorWithAlphaComponent:0];
    [self.view addSubview:self.sureBtn];
    [self.view addSubview:self.bgView];
    [self requestSetting];
}

- (UIImageView *)topBgImg {
    if(!_topBgImg){
        _topBgImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W,kWIDTH_RATIO*277)];
        _topBgImg.contentMode = UIViewContentModeScaleAspectFill;
        _topBgImg.image = [UIImage imageNamed:@"planBg"];
    }
    return _topBgImg;
}

- (UIButton *)sureBtn {
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureBtn.frame = CGRectMake(10, SCREEN_H - SafeAreaBottomHeight - 55, SCREEN_W - 20, 45);
        _sureBtn.backgroundColor = COLOR_TOPIC_SELECT;
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        [_sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _sureBtn.titleLabel.font  = PingFangMediumFont(18);

        _sureBtn.az_colors = @[(__bridge id)rgbHex(0x02A1F9, 1).CGColor,(__bridge id)rgbHex(0x29F19C,1).CGColor];
        _sureBtn.az_startPoint = CGPointMake(0, 0.5);
        _sureBtn.az_endPoint = CGPointMake(1, 0.5);
        [_sureBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_sureBtn addTarget:self action:@selector(sureBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
- (UIView *)bgView {
    if(!_bgView){
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(10, NavigationBarHeight + 20, SCREEN_W - 20, self.sureBtn.top - NavigationBarHeight -84)];
        _bgView.backgroundColor = [UIColor whiteColor];
        [_bgView js_setRoundedCorners:UIRectCornerAllCorners radii:15];
        [_bgView addSubview:self.planTopView];
        [_bgView addSubview:self.lineView];
        [_bgView addSubview:self.planTitle];
        [_bgView addSubview:self.greenBgView];
        [self setPickerVeiw];
    }
    return _bgView;
}

- (PlanView *)planTopView {
    if(!_planTopView){
        _planTopView = [[PlanView alloc]initWithFrame:CGRectMake(0, 15, self.bgView.width, 185)];
        _planTopView.backgroundColor = [UIColor whiteColor];
        
        [_planTopView setTitle:self.model.versionFullName num:self.model.wordsCount];
        
        [_planTopView updatePlanDay:@"" num:@""];
    }
    return _planTopView;
}
- (UIView *)lineView {
    if(!_lineView){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.planTopView.bottom , self.bgView.width, 0.5 )];
        
        _lineView.backgroundColor = rgbHex(0xe5e5e5, 1);
    }
    return _lineView;
}


- (UILabel *)planTitle {
    if(!_planTitle){
        _planTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, self.lineView.bottom + 15, self.lineView.width, 22)];
        _planTitle.text = @"我计划完成天数/每天背单词个数";
        _planTitle.textColor = UIColor.blackColor;
        _planTitle.font = PingFangMediumFont(16);
        _planTitle.textAlignment = NSTextAlignmentCenter;
    }
    return _planTitle;
}

- (UIButton *)greenBgView {
    if(!_greenBgView){
        _greenBgView = [UIButton buttonWithType:UIButtonTypeCustom];
        _greenBgView.userInteractionEnabled = NO;
        _greenBgView.frame = CGRectMake(self.bgView.width/2 - 110, self.planTitle.bottom + 14, 220, 28);
        
        [_greenBgView setImage:[UIImage imageNamed:@"clockIcon"] forState:UIControlStateNormal];
        [_greenBgView setTitle:@"" forState:UIControlStateNormal];
        
        [_greenBgView setTitleColor:COLOR_TOPIC_SELECT forState:UIControlStateNormal];
        _greenBgView.titleLabel.font  = PingFangMediumFont(12);
        
        _greenBgView.backgroundColor = [COLOR_TOPIC_SELECT colorWithAlphaComponent:0.1];
        [_greenBgView js_setRoundedCorners:UIRectCornerAllCorners radii:14];
    }
    return _greenBgView;
}

- (void)setPickerVeiw {
    self.myPickerView = [[DayNumPickerView alloc]initWithFrame:CGRectMake(0, 20 + self.greenBgView.bottom, self.bgView.width,self.bgView.height - self.greenBgView.bottom - 30)];
    self.myPickerView.allCount = self.model.wordsCount;
    WS(WeakSelf)
    self.myPickerView.dataChangeBlock = ^(NSString * _Nonnull day, NSString * _Nonnull count) {
        WeakSelf.days = day;
        WeakSelf.count = count;
        [WeakSelf planEndTimeWithDays:day count:count];
    };
    [self.bgView addSubview:self.myPickerView];
}

- (void)sureBtnClicked:(UIButton *)sender {
    [self setPlanRequest];
}


- (void)planEndTimeWithDays:(NSString *)day count:(NSString *)count {
    NSDate *current = [NSDate date];
    int dayNum = day.intValue;
    NSDate *endDate ;
    NSTimeInterval oneDay = 24*60*60;
    endDate = [current initWithTimeIntervalSinceNow:oneDay*dayNum];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"yyyy-MM-dd"];
    NSString * time = [NSString stringWithFormat:@"%@ ",[_formatter stringFromDate:endDate]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.greenBgView setTitle:[NSString stringWithFormat:@" 计划完成时间：%@",time]  forState:UIControlStateNormal];
        [self.planTopView updatePlanDay:day num:count];
    });
}


#pragma mark   请求
- (void)requestSetting {
    WS(WeakSelf)
    [BeiDanCiRequest request_getRecitingWithJiaoCaiid:self.model.versionId ListResult:^(NSMutableArray * _Nonnull dayList, NSMutableArray * _Nonnull numlist) {
        [WeakSelf.myPickerView updateDayList:dayList countList:numlist];
    }];
}

- (void)setPlanRequest {
    WS(WeakSelf)
    [BeiDanCiRequest request_postSetPlanWithVersion_Id:self.model.versionId days:self.days count:self.count reslut:^(BOOL isRequest) {
        if (isRequest) {
            ReciteViewController *reciteVC = [[ReciteViewController alloc]init];
            [WeakSelf.navigationController pushViewController:reciteVC animated:YES];
            
        }
    }];
}
@end
